package com.jams.auth.configure;

import com.jams.auth.security.JamsClientDetailsService;
import com.jams.auth.security.JamsUserDetailsServiceImpl;
import com.jams.common.core.constant.SecurityConstants;
import lombok.SneakyThrows;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;
import org.springframework.security.oauth2.provider.ClientDetailsService;

import javax.sql.DataSource;

/**
 * 认证服务器配置类
 */
@Configuration
@EnableAuthorizationServer
public class JamsAuthorizationServerConfigure extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private DataSource dataSource;
    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private JamsUserDetailsServiceImpl userDetailsService;
    /**
     * 配置令牌端点(Token Endpoint)的安全约束.
     */
    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
        //allowFormAuthenticationForClients() : 主要是让/oauth/token支持client_id以及client_secret作登录认证
        //Spring Security OAuth公开了两个用于检查令牌的端点(/oauth/check_token和/oauth/token_key).默认情况下不会公开这些端点(具有访问权限“denyAll()”).
        //tokenKeyAccess() : url:/oauth/token_key,如果使用JWT令牌，则暴露公钥以进行令牌验证
        //checkTokenAccess() :  url:/oauth/check_token 允许校验token
        security.allowFormAuthenticationForClients()
                .tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()");
    }
    @Override
    @SneakyThrows
    public void configure(ClientDetailsServiceConfigurer clients) {
        JamsClientDetailsService clientDetailsService = clientDetails();
        clientDetailsService.setSelectClientDetailsSql(SecurityConstants.DEFAULT_SELECT_STATEMENT);
        clientDetailsService.setFindClientDetailsSql(SecurityConstants.DEFAULT_FIND_STATEMENT);
        clients.withClientDetails(clientDetailsService);
    }


    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

        endpoints.allowedTokenEndpointRequestMethods(HttpMethod.GET, HttpMethod.POST)
                .authenticationManager(authenticationManager)
                .userDetailsService(userDetailsService)
                //.tokenStore(tokenStore);
                //.tokenServices(defaultTokenServices)
                ;
    }

    @Bean
    public JamsClientDetailsService clientDetails() {
        return new JamsClientDetailsService(dataSource);
    }

}
